import Vue from 'vue'
import Vuex from 'vuex'
import http from '../token'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    username:'',
  },
  getters: {
    getToken(state) {
      return state.token || sessionStorage.getItem('token') || ''
    },
    getUser(state) {
      return state.username || ''
    },
  },
  mutations: {
    setToken(state, token) {
      state.token = token
     
    },
  },
  actions: {
    setToken({ commit }, userinfo) {
      let url = '/login'
      http({ url, method: 'post', data: userinfo }).then(res => {
        // 存储在本地
        sessionStorage.setItem('token', res.token)

        commit('setToken', res.token)
      })
    },
  }
})
